<?php
class Bootstrap {

	function __construct() {
		$url = isset($_GET['url']) ?rtrim($_GET['url'],'/') : null;
		$url = explode('/',$url);
		if(empty($url[0])){
			require 'controllers/index.php';
			$controller = new index();
			$controller->index();
			return false;
		}

		$file = 'controllers/' . $url[0] . '.php';

		if(file_exists($file)){

			require $file;

		} else {
			require "controllers/error.php";
			$controller = new error();
			return false;
		}

		$controller = new $url[0];
		$controller->loadModel($url[0]);
		// call method
		if(isset($url[2])){
			if(method_exists($controller, $url[1])){
				$controller->{$url[1]}($url[2]);
			}else {
				require "controllers/error.php";
				$controller = new error();
				$controller->index();
				return false;
			}
		} else {
			if(isset($url[1])){
				if(method_exists($controller, $url[1])){
					$controller->{$url[1]}();
				}else {
					require "controllers/error.php";
					$controller = new error();
					$controller->index();
					return false;
				}
			}else {
				$controller->index();
			}
		}

	}// function __construct
	function error() {
		require 'controllers/error.php';
		$controller = new error();
		$controller->index();
		return false;
	}// end function error
}// End class Bootstrap
